package com.jdh.common;

import com.jdh.model.ChatMessage;
import com.jdh.model.User;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: Constant
 * @Author: jdh
 * @CreateTime: 2022-11-19
 * @Description: 该类主要是模拟数据库等存储的相关数据信息；
 * 如系统存在多少用户、每个用户存在好友和群列表、每个群用于的用户列表、用户与好友或者群对于的历史聊天信息等等
 * 如果是实际情况下，这些数据就不是像下面这样以静态的方法存储的，本demo主要不像纳入第三方功能，减少配置等等
 */
@Configuration
public class Constant {

    /**
     * 系统中所有存储的用户列表
     */
    public static final HashMap<String, User> allUserDataList = new HashMap<>();

    /**
     * 存储用户拥有的好友/群列表
     */
    public static final HashMap<String, List<String>> ownFriendList = new HashMap<>();

    /**
     * 存储群含有用户列表
     */
    public static final HashMap<String, List<String>> ownGroupList = new HashMap<>();

    /**
     * 所有用户列表
     */
    public static final List<String> allUserList = new ArrayList<>();

    /**
     * 在线用户列表
     */
    public static final List<String> onlineUserList = new ArrayList<>();

    /**
     * 存储当前已经连接上的用户，以连接的session为key
     */
    public static final HashMap<String, User> connectUserList = new HashMap<>();

    /**
     * 存储用户拥有的聊天消息(仅服务启动期间有效)
     */
    public static final HashMap<String, List<ChatMessage>> ownChatMsgList = new HashMap<>();

    /**
     * 下面主要是模拟初始化一些相关联的列表数据;
     * 实际情况这些一个是存在某个专门存储这些数据的中间件或者数据库中;
     * 然后他们直接的关联关系是通过各自的操作来完成的，比如添加好友、加群等等
     */
    @Bean
    public static void initChatRoomData() {
        allUserDataList.put("A001", new User("A001", "刘一", "1"));
        allUserDataList.put("A002", new User("A002", "陈二", "1"));
        allUserDataList.put("A003", new User("A003", "张三", "1"));
        allUserDataList.put("A004", new User("A004", "李四", "1"));
        allUserDataList.put("A005", new User("A005", "王五", "1"));
        allUserDataList.put("A006", new User("A006", "赵六", "1"));
        allUserDataList.put("A007", new User("A007", "孙七", "1"));
        allUserDataList.put("A008", new User("A008", "周八", "1"));
        allUserDataList.put("A009", new User("A009", "吴九", "1"));

        allUserList.addAll(Arrays.asList("A001", "A002", "A003", "A004", "A005", "A006", "A007", "A008", "A009"));

        allUserDataList.put("B001", new User("B001", "交流群1", "2"));
        allUserDataList.put("B002", new User("B002", "交流群2", "2"));
        allUserDataList.put("B003", new User("B003", "交流群3", "2"));

        //下面是对各个用户的好友列表初始化
        ownFriendList.put("A003", new ArrayList<>(
                Arrays.asList("A001", "A002", "A004", "A005", "A006", "A007", "A008", "A009", "B001", "B002", "B003")));
        ownFriendList.put("A004", new ArrayList<>(
                Arrays.asList("A001", "A002", "A003", "A005", "A006", "A007", "A008", "A009", "B001", "B002", "B003")));
        ownFriendList.put("A005", new ArrayList<>(
                Arrays.asList("A001", "A002", "A003", "A004", "A006", "A007", "A008", "A009", "B001", "B002", "B003")));

        ownFriendList.put("A001", new ArrayList<>(Arrays.asList("A003", "A004", "A005", "A002", "A006")));
        ownFriendList.put("A002", new ArrayList<>(Arrays.asList("A003", "A004", "A005", "A001")));
        ownFriendList.put("A006", new ArrayList<>(Arrays.asList("A003", "A004", "A005", "A001")));

        ownFriendList.put("A007", new ArrayList<>(Arrays.asList("A003", "A004", "A005", "A008", "A009", "B001", "B003")));
        ownFriendList.put("A008", new ArrayList<>(Arrays.asList("A003", "A004", "A005", "A007", "A009", "B001", "B002")));
        ownFriendList.put("A009", new ArrayList<>(Arrays.asList("A003", "A004", "A005", "A007", "A008", "B001", "B003")));

        //下面是对群成员初始化
        ownGroupList.put("B001", new ArrayList<>(Arrays.asList("A003", "A004", "A005", "A007", "A008", "A009")));
        ownGroupList.put("B002", new ArrayList<>(Arrays.asList("A003", "A004", "A005", "A008")));
        ownGroupList.put("B003", new ArrayList<>(Arrays.asList("A003", "A004", "A005", "A007", "A009")));

    }

    /**
     * 传入唯一标识的数据，无论在什么组合形式下，获取唯一标识
     * @param uuids
     * @return
     */
    public static String getOnlyTag(String ... uuids){
        String str = "";
        for (String uuid : uuids) {
            str += uuid;
        }
        char[] charArray = str.toCharArray();
        Arrays.sort(charArray);
        return DigestUtils.md5Hex(String.valueOf(charArray));
    }

}
